package com.losad.app.gateway.fitler;

import com.losad.app.common.util.net.ResponseUtil;
import com.losad.app.common.vo.JsonRe;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2016/8/26.
 */
public class ApiFilter extends ZuulFilter {


    private static Logger log = LoggerFactory.getLogger(ApiFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

        String accessToken = request.getHeader("token");
        if(accessToken == null || "".equals(accessToken.trim()) ) {
            log.error("access token is empty");
            log.info("=========================access token wrong:"+accessToken);
//////////////////////这里不做安全校验的处理/////////////////////////////////////////////
//            ctx.setResponseStatusCode(401);//未授权
//            ResponseUtil.flushJson(ctx.getResponse(), new JsonRe(-1, " access token is empty !! ",null));
////////////////////////////////////////////////////////////////////////////////////////
        }
       
        return null;
    }
}
